<?php
//$Id: mysqli.php 330 2012-04-29 10:13:25Z tomqin@gmail.com $

/**
 * Db_MySqli 类
 *
 * @copyright       Copyright (C) 2010-2012 ARESCMS Inc.
 * @author          TomQin <tomqin@gmail.com>
 * @license         http://www.arescms.cn/license/
 * @package Core
 */
class Db_MySqli extends MySqli{
    protected $options = array();
    public $queries = array();

    public function __construct(array $opts = array()) {
        $this->init_options($opts);

        parent::init(); // init mysqli for options, connection
        mysqli_report(MYSQLI_REPORT_OFF);

        if (!empty($this->options['timeout'])) {
            $ret = parent::options(MYSQLI_OPT_CONNECT_TIMEOUT, $this->options['timeout']);
        }
    }

    public function reconnect(){
        //connect database
        $ret = parent::real_connect($this->options['host'], $this->options['username'], $this->options['password'],
                                    $this->options['database'], $this->options['port'], $this->options['sock'],
                                    $this->options['flags']);
        if ($ret === false || $this->connect_errno) {
            throw new Exception($this->connect_error, $this->connect_errno);
        }
        if (!empty($this->options['charset'])) {
            parent::set_charset($this->options['charset']);
        }
        return $ret;
    }

    public function __destruct() {}

    public function query($sql) {
        $this->queries[] = $sql;
        $result = parent::query($sql);
        if(!$result || $this->errno) {
            throw new Exception($this->error, $this->errno);
        }else{
            return $result;
        }
    }

    public function prepare($sql){
        $this->queries[] = $sql;
        return parent::prepare($sql);
    }

    protected function init_options(array $opts) {
        $this->options = array();
        $this->options['server']     = 'prod'; // prod|qa|dev|etc

        $this->options['host']       = '127.0.0.1';
        $this->options['username']   = null;
        $this->options['password']   = null;
        $this->options['database']   = null;

        $this->options['port']       = 3306;
        $this->options['sock']       = null; // socket or named pipe
        $this->options['flags']      = null; // null ignore; MYSQLI_CLIENT_COMPRESS, etc

        $this->options['charset']    = null; // null ignore, else utf8, etc
        $this->options['timeout']    = null; // null ignore, else nbr secs

        $this->options = array_merge($this->options, $opts);
    }

    public function stmt_bind_param(&$stmt, $types, $param) {
        $var = array();
        foreach($param as $key => $ref) {
            $var[] = &$param[$key];
        }
        array_unshift($var, $types);
        call_user_func_array(array($stmt, 'bind_param'), $var);
        //$ref = new ReflectionMethod('mysqli_stmt', 'bind_param');
        //$ref->invokeArgs($stmt, $var);
    }
}
?>